const app = getApp()
const constant = require("../../../../../constant.js")
const storage = require("../../../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lisses: [
      { name: '快走', value: 'QUICK_WALK', selected: false },
      { name: '慢跑', value: 'JOGGING', selected: false},
      { name: '踏台阶', value: 'STEP', selected: false },
      { name: '爬楼', value: 'CLIMB', selected: false },
      { name: '游泳', value: 'SWIMMING', selected: false }
    ],
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      host: constant.api(app.globalData.pord).url,
      mobile: wx.getStorageSync(storage.keys.mobile),
      courseRoundId: options.courseRoundId,
      weeklyPlanId: options.weeklyPlanId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: "正在加载中..."
    })

    var that = this;

    wx.request({
      url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/weekly-plans/' + that.data.weeklyPlanId + '/exercise?mobile=' + that.data.mobile,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          wx.setNavigationBarTitle({
            title: res.data.data.name + '家庭作业'
          });

          that.setData({
            exercise: res.data.data.exercise ? res.data.data.exercise : null
          });

          if(that.data.exercise) {
            that.setData({
              lisses: that.data.exercise.lisses
            })
          }

          wx.hideLoading();
        } else {
          wx.hideLoading();

          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();

        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  checkboxChange: function (e) {
    let that = this;

    let checkedVal = e.detail.value;

    for (let i = 0; i < that.data.lisses.length; i++) {

      let element = that.data.lisses[i];

      let checkedId = element.value.toString();

      let index = checkedVal.indexOf(checkedId);

      if (index < 0) {

        element.selected = false;

      } else {

        element.selected = true;
      }

    }

    let lisses = 'lisses'
    that.setData({
      [lisses]: that.data.lisses
    })
  },

  formSubmit: function (e) {
    var that = this;
    that.setData({
      disabled: true
    });
    var data = e.detail.value;
    if (!data.homework_frequency) {
      that.setData({
        disabled: false
      });
      wx.showToast({
        title: '频率保证不能为空',
        icon: 'none',
        duration: 1000
      });
      return;
    } else if (data.liss_types.length == 0 && !data.other_liss.trim() && !data.hit.trim() && !data.core.trim()) {
      that.setData({
        disabled: false
      });
      wx.showToast({
        title: 'LISS,HIIT,核心训练请至少填写一项',
        icon: 'none',
        duration: 2000
      });
      return;
    } else {
      wx.request({
        url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/weekly-plans/' + that.data.weeklyPlanId + '/edit-exercise?mobile=' + that.data.mobile,
        method: 'POST',
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == "0") {
            wx.showToast({
              title: res.data.message,
              icon: 'success',
              duration: 1000,
              mask: true,
              success: function () {
                wx.navigateBack({
                  delta: 1
                });
              }
            });
          } else {
            wx.showToast({
              title: res.data.message ? res.data.message : '请求失败',
              icon: 'none',
              duration: 3000,
              mask: true
            })
            that.setData({
              disabled: false
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '保存失败',
            icon: 'none',
            duration: 3000,
            mask: true
          })
          that.setData({
            disabled: false
          })
        }
      });
    }
  }
})